import {useHttp} from "./http";
import {QueryKey, useMutation, useQuery} from "react-query";
import {Task} from "../types/task";
import {Project} from "../types/project";
import {
    useAddConfig,
    useDeleteConfig,
    useEditConfig,
    useReorderTaskConfig
} from "./use-optimistic-options";
import {SortProps} from "./kanban";

export const useTasks = (param?: Partial<Task>) => {
    const client = useHttp()
    return useQuery<Task[]>(['tasks', param],
        () => client('tasks', {data: param}))
}

export const useTask = (id?:number) => {
    const client = useHttp()
    return useQuery<Project>(
        ['task', [id]],
        () => client(`tasks/${id}`),
        {
            enabled: Boolean(id)
        }
    )
}

export const useEditTask = (queryKey: QueryKey) => {
    const client = useHttp()
    return useMutation(
        (params: Partial<Task>) =>
            client(`tasks/${params.id}`, {
                method: 'PATCH',
                data: params
            }),
        useEditConfig(queryKey)
    )
}

export const useAddTask = (queryKey: QueryKey) => {
    const client = useHttp()

    return useMutation(
        (params: Partial<Task>) =>
            client(`tasks`, {
                data: params,
                method: 'POST'
            }),
        useAddConfig(queryKey)
    )
}

export const useDeleteTask = (queryKey: QueryKey) => {
    const client = useHttp()
    return useMutation(
        ({id}: {id: number}) =>
            client(`tasks/${id}`, {
                method: 'DELETE'
            }),
        useDeleteConfig(queryKey)
    )
}

export const useRecorderTask = (queryKey: QueryKey) => {
    const client = useHttp()
    return useMutation((params: SortProps) => {
            return client('tasks/reorder', {
                data: params,
                method: 'POST'
            })
        },
        useReorderTaskConfig(queryKey)
    )
}
